package com.ys.controller.api.purchase;

import com.ys.config.MenuId;
import com.ys.entity.purchase.TcgDistribution;
import com.ys.entity.sys.YsCol;
import com.ys.service.purchase.TcgDistributionService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: fxz
 * @CreateTime: 2024-09-23
 * @Description: 1688铺货
 * @Version: 1.0
 */
@Controller
@RequestMapping("/api/inter/tcgDistribution")
public class ApiInterTcgDistributionController {
	@Autowired
	private TcgDistributionService tcgDistributionService;

	@Autowired
	private CommonApiService<TcgDistribution> commonApiService;


	@RequiresPermissions("tcgDistribution:view")
	@RequestMapping("/search")
	@ResponseBody
	public LayuiTableVoJA search(@ModelAttribute TcgDistribution cond, String conditions){

		cond = setSeachSql(cond, conditions);

		return commonApiService.searchLay(cond);
	}

	/**
	 * 设置查询的cond的sql设置
	 * @param cond
	 * @return
	 */
	public TcgDistribution setSeachSql(TcgDistribution cond, String conditions){

		return  cond;
	}


	/**
	 * 修改
	 */
	@RequiresPermissions("tcgDistribution:edit")
	@PostMapping("/edit")
	@ResponseBody
	public R edit(@RequestParam Map<String, Object> params){
		try {
			return tcgDistributionService.edit(params);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error(e.getMessage());
		}
	}

	/**
	 * 修改
	 */
	@RequiresPermissions("tcgDistribution:edit")
	@PostMapping("/editZj")
	@ResponseBody
	public R editZj(@RequestParam Map<String, Object> params){
		try {
			return tcgDistributionService.editZj(params);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error(e.getMessage());
		}
	}

	/**
	 * 导出
	 */
	@RequestMapping("/export")
	@ResponseBody
	public R exportByArchives(@ModelAttribute TcgDistribution cond, String conditions){
		try {
			cond.setPage(0).setSize(999999);
			cond = setSeachSql(cond,conditions);
			//获取菜单字段数据
			List<YsCol> listField = new YsCol()
					.setMenuId(MenuId.TCG_Distribution)
					.setFrontShow(1)
					.setOrderby("A.SortNo asc")
					.queryList();
			//获取数据
			List<Map<String, Object>> listData = cond.queryMap();
			//处理数据
			ExportExcelUtils<TcgDistribution> util = new ExportExcelUtils<>(new TcgDistribution());
			String toFilePath = util.exportExcel(listData, listField, "1688铺货");
			UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
			return R.ok(dto.getUrl());
		}catch (Exception e){
			return R.error("导出失败，请稍后重试！");
		}
	}
}
